using System;
using System.IO;
using System.Text;
using System.Xml;

using DigitalRune.Windows.TextEditor.Completion;

namespace Zove.Modeling.Editor {
    /// <summary>
    /// Represents an item in the code completion window.
    /// </summary>
    class CodeCompletionData : DefaultCompletionData, ICompletionData {
        public CodeCompletionData()
            : base("", null, 0) {
        }

        int overloads = 0;

        public void AddOverload() {
            overloads++;
        }

        string description;

        // DefaultCompletionData.Description is not virtual, but we can reimplement
        // the interface to get the same effect as overriding.
        string ICompletionData.Description {
            get {
                description = "Hello";
                return description;
            }
        }

        public static string XmlDocumentationToText(string xmlDoc) {
            System.Diagnostics.Debug.WriteLine(xmlDoc);
            StringBuilder b = new StringBuilder();
            try {
                using (XmlTextReader reader = new XmlTextReader(new StringReader("<root>" + xmlDoc + "</root>"))) {
                    reader.XmlResolver = null;
                    while (reader.Read()) {
                        switch (reader.NodeType) {
                            case XmlNodeType.Text:
                                b.Append(reader.Value);
                                break;
                            case XmlNodeType.Element:
                                switch (reader.Name) {
                                    case "filterpriority":
                                        reader.Skip();
                                        break;
                                    case "returns":
                                        b.AppendLine();
                                        b.Append("Returns: ");
                                        break;
                                    case "param":
                                        b.AppendLine();
                                        b.Append(reader.GetAttribute("name") + ": ");
                                        break;
                                    case "remarks":
                                        b.AppendLine();
                                        b.Append("Remarks: ");
                                        break;
                                    case "see":
                                        if (reader.IsEmptyElement) {
                                            b.Append(reader.GetAttribute("cref"));
                                        } else {
                                            reader.MoveToContent();
                                            if (reader.HasValue) {
                                                b.Append(reader.Value);
                                            } else {
                                                b.Append(reader.GetAttribute("cref"));
                                            }
                                        }
                                        break;
                                }
                                break;
                        }
                    }
                }
                return b.ToString();
            } catch (XmlException) {
                return xmlDoc;
            }
        }
    }
}
